package cn.insect.service.impl;

import cn.insect.dao.InsectMapper;
import cn.insect.pojo.Insect;
import cn.insect.service.InsectService;
import cn.insect.utils.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @auth 吴阳基
 * @date 2024/1/3 15:23
 */
public class InsectServiceImpl implements InsectService {
    private SqlSession sqlSession = MyBatisUtil.createSqlSession();
    private InsectMapper mapper = sqlSession.getMapper(InsectMapper.class);

    @Override
    public List<Insect> getSelect(String name, int cid) {
        return mapper.getSelect(name, cid);
    }

    @Override
    public int add(Insect insect) {
        int result = 0;
        try {
            result = mapper.add(insect);
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
            result = 0;
            sqlSession.rollback();
        }
        return result;
    }

    @Override
    public int del(int id) {
        int result = 0;
        try {
            result = mapper.del(id);
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
            result = 0;
            sqlSession.rollback();
        }
        return result;
    }
}
